﻿/**********************************天使物流*******************************************
* 功能描述:结算管理-进项发票管理
* 作者:石锦涛
* 时间:2022-01-11
* 
**********************************天使物流*******************************************/
using Snowflake.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TianShiProject.EntityFrameworkCore;
using TianShiProject.Models.Common;
using TianShiProject.Models.SettleAccounts.NewInputInvoices;
using Volo.Abp.EntityFrameworkCore;

namespace TianShiProject.SettleAccounts.NewInputInvoices
{
    public class NewInputAppRepository : DapperDbContextRepository, INewInputAppRepository
    {
        public NewInputAppRepository(IDbContextProvider<TianShiProjectDbContext> dbContextProvider) : base(dbContextProvider)
        {
        }

        /// <summary>
        /// 分页显示
        /// </summary>
        /// <returns></returns>
        public async Task<PageResponse<NewInputInvoiceDto>> GetNewInput(string name, int? clas, string times, int pageindex = 1, int pagesize = 5)
        {
            try
            {
                var sql = "select Id as GId,InvoiceNumber,InvoiceUnit,InvoiceType,InvoiceMoney,TaxRate,TaxQuantity,SUBSTRING(CONVERT(Nvarchar(50),MakeInvoiceTime),1,10) as MakeInvoiceTime,Remarkz,PreparedDyr,SUBSTRING(CONVERT(Nvarchar(50), EstablishTimes), 1, 10) as EstablishTimes,IsDelete from NewInputInvoice where IsDelete = 0  ";
                if(!string.IsNullOrEmpty(name))
                {
                    sql += $" and InvoiceUnit like '%{name}%'";
                }
                if (clas>0)
                {
                    sql += $" and InvoiceType = '{clas}'";
                }
                if (!string.IsNullOrEmpty(times))
                {
                    sql += $" and MakeInvoiceTime = '{times}'";
                }

                PageResponse<NewInputInvoiceDto> doc = new PageResponse<NewInputInvoiceDto>();
                var list = await this.QueryAsync<NewInputInvoiceDto>(sql,null);
                doc.PageTotal = list.Count();
                sql += $" order by Id offset ({pageindex}-1)*{pagesize} rows fetch next {pagesize} rows only";
                var query = await this.QueryAsync<NewInputInvoiceDto>(sql, null);
                doc.PageData = query;
                return doc;

            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        ///  进项发票逻辑删除
        /// </summary>
        /// <returns></returns>
        public async Task<PageResponse<NewInputInvoiceDto>> DeleteNewInput(int Id)
        {
            try
            {
                PageResponse<NewInputInvoiceDto> doc = new PageResponse<NewInputInvoiceDto>();
                var sql = $"update  NewInputInvoice set IsDelete=1 where Id={Id}";
                doc.Code = await this.ExecuteAsync(sql, null);
                return doc;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 修改进项发票
        /// </summary>
        /// <param name="newInput"></param>
        /// <returns></returns>
        public async Task<PageResponse<int>> UpdateNewInput(NewInputInvoiceDto newInput)
        {
            try
            {
                var sql = $"update  NewInputInvoice set InvoiceNumber='{newInput.InvoiceNumber}',InvoiceUnit='{newInput.InvoiceUnit}',InvoiceType='{newInput.InvoiceType}',InvoiceMoney='{newInput.InvoiceMoney}',TaxRate='{newInput.TaxRate}',TaxQuantity = '{newInput.TaxQuantity}',MakeInvoiceTime = '{newInput.MakeInvoiceTime.ToString("yyyy/MM/dd")}',Remarkz = '{newInput.Remarkz}',PreparedDyr = '{newInput.PreparedDyr}',EstablishTimes = '{newInput.EstablishTimes.ToString("yyyy/MM/dd")}',IsDelete = '{newInput.IsDelete}' where Id = '{newInput.GId}'";
                PageResponse<int> doc = new PageResponse<int>();

                doc.Code = await this.ExecuteAsync(sql, null);
                return doc;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 新增进项发票
        /// </summary>
        /// <param name="newInput"></param>
        /// <returns></returns>
        public async Task<PageResponse<int>> PostNewInput(NewInputInvoiceDto newInput)
        {
            try
            {
                //雪花算法
                var worker = new IdWorker(1, 1);
                var id = worker.NextId().ToString();

                var sql = $"insert NewInputInvoice values('{id}','{newInput.InvoiceUnit}','{newInput.InvoiceType}','{newInput.InvoiceMoney}','{newInput.TaxRate}','{newInput.TaxQuantity}','{newInput.MakeInvoiceTime.ToString("yyyy/MM/dd")}','{newInput.Remarkz}','{newInput.PreparedDyr}',GETDATE(),'{newInput.IsDelete}')";
                PageResponse<int> doc = new PageResponse<int>();
                doc.Code = await this.ExecuteAsync(sql, null);
                return doc;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 批量逻辑删除进项发票
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<PageResponse<NewInputInvoiceDto>> DeleteNewInputMent(string Id)
        {
            try
            {
                PageResponse<NewInputInvoiceDto> doc = new PageResponse<NewInputInvoiceDto>();
                var ids = Id.Split(',');
                foreach (var item in ids)
                {
                    var shu = Convert.ToInt32(item);
                    var list = await this.QueryAsync<NewInputInvoiceDto>($"select * from NewInputInvoice where Id={shu}");
                    if (list != null)
                    {
                        var sql = $"update NewInputInvoice set IsDelete=1 where Id={shu} ";
                        doc.Code = await this.ExecuteAsync(sql, null);
                    }
                }
                return doc;
            }
            catch (Exception)
            {

                throw;
            }
        }

    }
}
